package com.example.util;

import okhttp3.*;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class HttpClientUtil {

    public String call(String url) throws IOException {
        // 1. 创建HttpClient实例
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 2. 创建GET请求方法实例
        HttpGet httpGet = new HttpGet(url);
        // 3. 调用HttpClient实例来执行GET请求方法，得到response
        CloseableHttpResponse response = httpClient.execute(httpGet);
        // 4. 读response，判断是否访问成功 2xx表示 成功。
        int status = response.getStatusLine().getStatusCode();
        String res = "";
        if (status == 200) {
            // 对得到后的实例可以进行处理，例如读取回复体，读取html
            HttpEntity entity = response.getEntity();
            res = EntityUtils.toString(entity);
            System.out.println("远程调用--->" + res);
        } else {
            throw new ClientProtocolException("Unexpected response status: " + status);
        }

        // 6. 释放连接
        response.close();
        httpClient.close();
        return res;
    }

    public String call2(String url) {
        Request request = new Request.Builder()
                .get()
                .url(url)
                .build();
        OkHttpClient client = new OkHttpClient();
        String responseStr = "";
        try {
            final Call call = client.newCall(request);
            Response response = call.execute();
            if (response.isSuccessful()){
                responseStr = response.body().string();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return responseStr;
    }
}
